package jee.boot.module.websocket;

import jee.boot.common.utils.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

/**
 * 后台管理系统的推送
 */
@ServerEndpoint(value = "/websocket/{sid}")
@Component
public class WebSocketConnect {
    /**
     * log4j日志
     */
    private static final Logger log = LoggerFactory.getLogger(WebSocketConnect.class);

    private WebSocketHandler webSocketHandler;

    @OnOpen
    public void openSession(@PathParam("sid") String sid, Session session) {
        getWebSocketHandler().onOpen(session,sid);
    }
    @OnMessage
    public void onMessage(@PathParam("sid") String sid, String message,Session session) {
        getWebSocketHandler().onMessage(session,sid,message);
    }

    @OnClose
    public void onClose(@PathParam("sid") String sid, Session session) {
        getWebSocketHandler().onClose(session,sid);
    }
    @OnError
    public void onError(@PathParam("sid") String sid,Session session, Throwable throwable) {
        getWebSocketHandler().onError(session,sid,throwable);
    }
    private WebSocketHandler getWebSocketHandler(){
        if(this.webSocketHandler==null){
            this.webSocketHandler=SpringContextHolder.getApplicationContext().getBean(WebSocketHandler.class);
        }
        return webSocketHandler;
    }

}